var xml2js = require('xml2js');//xml转josn格式数据
var parser = new xml2js.Parser();
var Promise = require("bluebird");
exports.parserXML = function (xml) {
  return new Promise(function (resolve, reject) {
    xml2js.parseString(xml, { trim: true }, function (err, content) {
      if (err) {
        reject(err);
      } else {
        resolve(content)
      }
    })
  })
};
exports.fomatexml = function (xml) {
  return new Promise(function (resolve, reject) {
    xml2js.parseString(xml, { trim: true }, function (err, content) {
      if (err) {
        reject(err);
      } else {
        resolve(content)
      }
    })
  })
};

function fomatexml(xml) {
  var message = {};
  if (typeof xml == "obj") {
    var keys = Object.keys(xml);
    for (var index = 0; index < keys.length; index++) {
      var item = xml[keys[index]];
      var key = keys[index];
      if (!(item instanceof Array) || item.length === 0) {
        continue
      }
      if (item.length === 1) {
        var val = item[0]
        if (typeof val == "object") {
          message[key] = fomatexml(val)
        } else {
          message[key] = (val || '').trim();
        }
      } else {
        message[key] = [];
        for (var a = 0, b = item.length; a < b; a++) {
          message[key].push(fomatexml(item[a]))
        }
      }
    }
  }
}
exports.auth = require('node-weixin-auth');
exports.oauth = require('node-weixin-oauth');
exports.media = require('node-weixin-media');
exports.pay = require('node-weixin-pay');
exports.menu = require('node-weixin-menu');
exports.link = require('node-weixin-link');
exports.jssdk = require('node-weixin-jssdk');
exports.user = require('node-weixin-user');
exports.message = require('node-weixin-message');
